/*++

Copyright (c) 2007  Microsoft Corporation 

Filename: 

    FrmDataSummary.cs

Abstract: 

    Form to implement the wizard option using datagridview
 
--*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using Visio = Microsoft.Office.Interop.Visio;
using System.Xml;
using UtilityManager;
using ExcelManager;
using System.IO;
using AddIn.Windows.Forms;
using ADODB;
using System.Reflection;
using System.Globalization;

namespace RackServerManager
{
    public partial class FrmDataSummary : AddIn.Windows.Forms.InteriorWizardPage
    {
        #region Variables
        private DataTable dtColo;
        private DataTable dtRack;
        private DataTable dtServer;
        #endregion 

        #region Form Variables
        private System.Windows.Forms.DataGridViewTextBoxColumn colRack2;
        private System.Windows.Forms.DataGridViewTextBoxColumn colColo2;
        private System.Windows.Forms.DataGridViewTextBoxColumn colPower2;
        private System.Windows.Forms.DataGridViewTextBoxColumn colMaxUnits;
        private System.Windows.Forms.DataGridView serverGridView;
        private System.Windows.Forms.DataGridViewTextBoxColumn colNetwork;
        private System.Windows.Forms.DataGridViewTextBoxColumn colRack;
        private System.Windows.Forms.DataGridViewTextBoxColumn colColo;
        private System.Windows.Forms.DataGridViewTextBoxColumn colPower;
        private System.Windows.Forms.DataGridViewTextBoxColumn colRackSpace;
        private System.Windows.Forms.DataGridView rackGridView;
        private System.Windows.Forms.DataGridView coloGridView;
        private System.Windows.Forms.TabPage tbPageServer;
        private System.Windows.Forms.TabControl tbCtrlDataSummary;
        private System.Windows.Forms.TabPage tbPageRack;
        private System.Windows.Forms.TabPage tbPageColo;
        private System.Windows.Forms.Button btnExportExcel;
        private System.Windows.Forms.Label m_subtitleLabel;
        private System.Windows.Forms.Label titleLabel;
        private System.Windows.Forms.DataGridViewTextBoxColumn colColo3;
        private System.Windows.Forms.DataGridViewTextBoxColumn colMaxColoUnits;
        #endregion 

        #region Constructor
        /// <summary>
        /// Default Constructor
        /// </summary>
        public FrmDataSummary()
        {
            InitializeComponent();
            this.titleLabel.Text = Global.GetResourceString("FormSummaryTitle_Text");
            this.m_subtitleLabel.Text = Global.GetResourceString("FormSummarySubTitle_Text");
            this.btnExportExcel.Text = Global.GetResourceString("FormSummaryExportExcel_Btn");
            this.tbPageServer.Text = Global.GetResourceString("ServerTab3_Text");
            this.tbPageRack.Text = Global.GetResourceString("RackTab2_Text");
            this.tbPageColo.Text = Global.GetResourceString("ColoTab1_Text");           
            this.colNetwork.HeaderText = Global.GetResourceString("GridServerColNetwork");        
            this.colRack.HeaderText = Global.GetResourceString("GridServerColRack");           
            this.colColo.HeaderText = Global.GetResourceString("GridServerColColo");           
            this.colPower.HeaderText = Global.GetResourceString("GridServerColPower");        
            this.colRackSpace.HeaderText = Global.GetResourceString("GridServerColRackSpace");          
            this.colRack2.HeaderText = Global.GetResourceString("GridServerColRack");        
            this.colColo2.HeaderText = Global.GetResourceString("GridServerColColo");         
            this.colPower2.HeaderText = Global.GetResourceString("GridServerColPower");         
            this.colMaxUnits.HeaderText = Global.GetResourceString("GridServerColMaxUnits");            
            this.colColo3.HeaderText = Global.GetResourceString("GridServerColColo");           
            this.colMaxColoUnits.HeaderText = Global.GetResourceString("GridServerColMaxUnits");
                      
            this.HeaderPanel.Controls.Add(this.titleLabel);
            this.HeaderPanel.Controls.Add(this.m_subtitleLabel);
            this.HeaderPanel.ResumeLayout(false);

        }
        #endregion

        #region Wizard Methods
        /// <summary>
        /// On Form Active
        /// </summary>
        /// <returns></returns>
        protected override bool OnSetActive()
        {
            if (!base.OnSetActive())
                return false;

            // Enable both the Next and Back buttons on this page    
            Wizard.SetWizardButtons(WizardButtons.Back | WizardButtons.Finish);
            return true;
        }

        /// <summary>
        /// On Back clicked
        /// </summary>
        /// <returns></returns>
        protected override string OnWizardBack()
        {
            // Form Import
            return "FrmImport";
        }

        /// <summary>
        /// On Finish clicked
        /// </summary>
        /// <returns></returns>
        protected override bool OnWizardFinish()
        {
            // Get datagridview rows into DataGridViewRowCollection
            DataGridViewRowCollection dgvrcColo = coloGridView.Rows;
            DataGridViewRowCollection dgvrcRack = rackGridView.Rows;
            DataGridViewRowCollection dgvrcServer = serverGridView.Rows;
            
            try
            {
                // Check for colo grid consists of any blank columns
                if (dgvrcColo.Count > 1)
                {
                    for (int coloRowIndex = 0; coloRowIndex < dgvrcColo.Count - 1; coloRowIndex++)
                    {
                        for (int coloColIndex = 0; coloColIndex < coloGridView.Columns.Count; coloColIndex++)
                        {
                            if (String.IsNullOrEmpty((string)dgvrcColo[coloRowIndex].Cells[coloColIndex].Value))
                            {
                                // If column is blank
                                MessageBox.Show(Global.GetResourceString("DoubleQuote")
                                    + dgvrcColo[coloRowIndex].Cells[coloColIndex].OwningColumn.HeaderText
                                    + Global.GetResourceString("DoubleQuote") +
                                      Global.GetResourceString("Blank_Values"),
                                      Global.GetResourceString("MessageBoxCaption"),
                                      MessageBoxButtons.OK,
                                      MessageBoxIcon.Warning,
                                      MessageBoxDefaultButton.Button1
                                      );                                
                                // Set tab page focus
                                tbCtrlDataSummary.SelectedIndex = 0;
                                return false;
                            }
                        }
                    }
                }
                else
                {
                    // If column is blank
                    MessageBox.Show(Global.GetResourceString("DoubleQuote")
                        + dgvrcColo[0].Cells[0].OwningColumn.HeaderText
                        + Global.GetResourceString("DoubleQuote") +
                          Global.GetResourceString("Blank_Values"),
                          Global.GetResourceString("MessageBoxCaption"),
                          MessageBoxButtons.OK,
                          MessageBoxIcon.Warning,
                          MessageBoxDefaultButton.Button1
                          );                    
                    // Set tab page focus
                    tbCtrlDataSummary.SelectedIndex = 0;
                    return false;
                }

                // Check for rack grid consists of any blank columns
                if (dgvrcRack.Count > 1)
                {
                    for (int rackRowIndex = 0; rackRowIndex < dgvrcRack.Count - 1; rackRowIndex++)
                    {
                        for (int rackColIndex = 0; rackColIndex < rackGridView.Columns.Count; rackColIndex++)
                        {
                            if (String.IsNullOrEmpty((string)dgvrcRack[rackRowIndex].Cells[rackColIndex].Value))
                            {
                                // If column is blank
                                MessageBox.Show(Global.GetResourceString("DoubleQuote")
                                    + dgvrcRack[rackRowIndex].Cells[rackColIndex].OwningColumn.HeaderText
                                    + Global.GetResourceString("DoubleQuote") +
                                      Global.GetResourceString("Blank_Values"),
                                      Global.GetResourceString("MessageBoxCaption"),
                                      MessageBoxButtons.OK,
                                      MessageBoxIcon.Warning,
                                      MessageBoxDefaultButton.Button1
                                      );                                
                                // Set tab page focus
                                tbCtrlDataSummary.SelectedIndex = 1;
                                return false;
                            }
                        }
                    }
                }
                else
                {
                    // If column is blank
                    MessageBox.Show(Global.GetResourceString("DoubleQuote")
                        + dgvrcRack[0].Cells[0].OwningColumn.HeaderText
                        + Global.GetResourceString("DoubleQuote") +
                          Global.GetResourceString("Blank_Values"),
                          Global.GetResourceString("MessageBoxCaption"),
                          MessageBoxButtons.OK,
                          MessageBoxIcon.Warning,
                          MessageBoxDefaultButton.Button1
                          );                   
                    // Set tab page focus
                    tbCtrlDataSummary.SelectedIndex = 1;
                    return false;

                }

                // Check for server grid consists of any blank columns
                if (dgvrcServer.Count > 1)
                {
                    for (int serverRowIndex = 0; serverRowIndex < dgvrcServer.Count - 1; serverRowIndex++)
                    {
                        for (int serverColIndex = 0; serverColIndex < serverGridView.Columns.Count; serverColIndex++)
                        {
                            if (String.IsNullOrEmpty((string)dgvrcServer[serverRowIndex].Cells[serverColIndex].Value))
                            {
                                // If column is blank
                                MessageBox.Show(Global.GetResourceString("DoubleQuote")
                                    + dgvrcServer[serverRowIndex].Cells[serverColIndex].OwningColumn.HeaderText
                                    + Global.GetResourceString("DoubleQuote") +
                                      Global.GetResourceString("Blank_Values"),
                                      Global.GetResourceString("MessageBoxCaption"),
                                      MessageBoxButtons.OK,
                                      MessageBoxIcon.Warning,
                                      MessageBoxDefaultButton.Button1
                                      );                                
                                // Set tab page focus
                                tbCtrlDataSummary.SelectedIndex = 2;
                                return false;
                            }
                        }
                    }
                }
                else
                {
                    // If column is blank
                    MessageBox.Show(Global.GetResourceString("DoubleQuote")
                        + dgvrcServer[0].Cells[0].OwningColumn.HeaderText
                        + Global.GetResourceString("DoubleQuote")
                        + Global.GetResourceString("Blank_Values"),
                          Global.GetResourceString("MessageBoxCaption"),
                          MessageBoxButtons.OK,
                          MessageBoxIcon.Warning,
                          MessageBoxDefaultButton.Button1
                          );                 
                    // Set tab page focus
                    tbCtrlDataSummary.SelectedIndex = 2;
                    return false;
                }

                // Create datatable from datagridview so that it can be used for ado xml conversion 
                dtColo = CreateDataTableFromColoGrid();
                dtRack = CreateDataTableFromRackGrid();
                dtServer = CreateDataTableFromServerGrid();

                // Call the conversion function to get the ADO xml
                Global.AdoColoXml = ConvertToAdoRecordsetXml(dtColo);
                Global.AdoRackXml = ConvertToAdoRecordsetXml(dtRack);
                Global.AdoServerXml = ConvertToAdoRecordsetXml(dtServer);

                // Flag set to create datarecordset
                Global.RecordSetCreated = true;

                return true;
            }
            catch (RackException ex)
            {

                // To catch and display exceptions from sub functions
                RackException.HandleExceptions(Global.GetResourceString("Exception_MsgBox_Caption"),
                            Global.GetResourceString("Exception_UsrMsg"), ex.InnerException);

                return false;
            }
            catch (Exception ex)
            {
                // To catch exceptions from current functions
                RackException.HandleExceptions(Global.GetResourceString("Exception_MsgBox_Caption"),
                            Global.GetResourceString("Exception_UsrMsg"), ex);

                return false;
            }
        }
        #endregion

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
            this.rackGridView = new System.Windows.Forms.DataGridView();
            this.colRack2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.colColo2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.colPower2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.colMaxUnits = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.coloGridView = new System.Windows.Forms.DataGridView();
            this.colColo3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.colMaxColoUnits = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.tbPageServer = new System.Windows.Forms.TabPage();
            this.serverGridView = new System.Windows.Forms.DataGridView();
            this.colNetwork = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.colRack = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.colColo = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.colPower = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.colRackSpace = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.tbCtrlDataSummary = new System.Windows.Forms.TabControl();
            this.tbPageColo = new System.Windows.Forms.TabPage();
            this.tbPageRack = new System.Windows.Forms.TabPage();
            this.btnExportExcel = new System.Windows.Forms.Button();
            this.m_subtitleLabel = new System.Windows.Forms.Label();
            this.titleLabel = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBoxIcon)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.rackGridView)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.coloGridView)).BeginInit();
            this.tbPageServer.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.serverGridView)).BeginInit();
            this.tbCtrlDataSummary.SuspendLayout();
            this.tbPageColo.SuspendLayout();
            this.tbPageRack.SuspendLayout();
            this.SuspendLayout();
            // 
            // rackGridView
            // 
            this.rackGridView.AllowDrop = true;
            this.rackGridView.AllowUserToResizeColumns = false;
            this.rackGridView.AllowUserToResizeRows = false;
            this.rackGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.rackGridView.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
            dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
            dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
            dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
            dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
            dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
            dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
            this.rackGridView.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
            this.rackGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.rackGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.colRack2,
            this.colColo2,
            this.colPower2,
            this.colMaxUnits});
            this.rackGridView.Location = new System.Drawing.Point(0, 31);
            this.rackGridView.Name = "rackGridView";
            this.rackGridView.RowHeadersVisible = false;
            this.rackGridView.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
            this.rackGridView.Size = new System.Drawing.Size(466, 74);
            this.rackGridView.TabIndex = 17;
            // 
            // colRack2
            // 
            this.colRack2.Name = "colRack2";
            // 
            // colColo2
            // 
            this.colColo2.Name = "colColo2";
            // 
            // colPower2
            // 
            this.colPower2.Name = "colPower2";
            // 
            // colMaxUnits
            // 
            this.colMaxUnits.Name = "colMaxUnits";
            // 
            // coloGridView
            // 
            this.coloGridView.AllowDrop = true;
            this.coloGridView.AllowUserToDeleteRows = false;
            this.coloGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.coloGridView.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
            this.coloGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.coloGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.colColo3,
            this.colMaxColoUnits});
            this.coloGridView.Location = new System.Drawing.Point(0, 31);
            this.coloGridView.Name = "coloGridView";
            this.coloGridView.RowHeadersVisible = false;
            this.coloGridView.Size = new System.Drawing.Size(466, 74);
            this.coloGridView.TabIndex = 0;
            // 
            // colColo3
            // 
            this.colColo3.Name = "colColo3";
            // 
            // colMaxColoUnits
            // 
            this.colMaxColoUnits.Name = "colMaxColoUnits";
            // 
            // tbPageServer
            // 
            this.tbPageServer.Controls.Add(this.serverGridView);
            this.tbPageServer.Location = new System.Drawing.Point(4, 22);
            this.tbPageServer.Name = "tbPageServer";
            this.tbPageServer.Padding = new System.Windows.Forms.Padding(3);
            this.tbPageServer.Size = new System.Drawing.Size(466, 173);
            this.tbPageServer.TabIndex = 0;
            this.tbPageServer.UseVisualStyleBackColor = true;
            // 
            // serverGridView
            // 
            this.serverGridView.AllowUserToResizeRows = false;
            this.serverGridView.Anchor = System.Windows.Forms.AnchorStyles.Left;
            this.serverGridView.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
            dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
            dataGridViewCellStyle2.BackColor = System.Drawing.SystemColors.Control;
            dataGridViewCellStyle2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            dataGridViewCellStyle2.ForeColor = System.Drawing.SystemColors.WindowText;
            dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
            dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
            this.serverGridView.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle2;
            this.serverGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.serverGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.colNetwork,
            this.colRack,
            this.colColo,
            this.colPower,
            this.colRackSpace});
            this.serverGridView.Location = new System.Drawing.Point(0, 31);
            this.serverGridView.Name = "serverGridView";
            this.serverGridView.RowHeadersVisible = false;
            this.serverGridView.Size = new System.Drawing.Size(466, 74);
            this.serverGridView.TabIndex = 0;
            // 
            // colNetwork
            // 
            this.colNetwork.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
            this.colNetwork.FillWeight = 109.9239F;
            this.colNetwork.Name = "colNetwork";
            // 
            // colRack
            // 
            this.colRack.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
            this.colRack.FillWeight = 91.37057F;
            this.colRack.Name = "colRack";
            // 
            // colColo
            // 
            this.colColo.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
            this.colColo.FillWeight = 89.05579F;
            this.colColo.Name = "colColo";
            // 
            // colPower
            // 
            this.colPower.FillWeight = 96.79675F;
            this.colPower.Name = "colPower";
            // 
            // colRackSpace
            // 
            this.colRackSpace.FillWeight = 112.8531F;
            this.colRackSpace.Name = "colRackSpace";
            // 
            // tbCtrlDataSummary
            // 
            this.tbCtrlDataSummary.Controls.Add(this.tbPageColo);
            this.tbCtrlDataSummary.Controls.Add(this.tbPageRack);
            this.tbCtrlDataSummary.Controls.Add(this.tbPageServer);
            this.tbCtrlDataSummary.Location = new System.Drawing.Point(12, 78);
            this.tbCtrlDataSummary.Name = "tbCtrlDataSummary";
            this.tbCtrlDataSummary.SelectedIndex = 0;
            this.tbCtrlDataSummary.Size = new System.Drawing.Size(474, 199);
            this.tbCtrlDataSummary.TabIndex = 1;
            // 
            // tbPageColo
            // 
            this.tbPageColo.Controls.Add(this.coloGridView);
            this.tbPageColo.Location = new System.Drawing.Point(4, 22);
            this.tbPageColo.Name = "tbPageColo";
            this.tbPageColo.Size = new System.Drawing.Size(466, 173);
            this.tbPageColo.TabIndex = 2;
            this.tbPageColo.UseVisualStyleBackColor = true;
            // 
            // tbPageRack
            // 
            this.tbPageRack.Controls.Add(this.rackGridView);
            this.tbPageRack.Location = new System.Drawing.Point(4, 22);
            this.tbPageRack.Name = "tbPageRack";
            this.tbPageRack.Padding = new System.Windows.Forms.Padding(3);
            this.tbPageRack.Size = new System.Drawing.Size(466, 173);
            this.tbPageRack.TabIndex = 1;
            this.tbPageRack.UseVisualStyleBackColor = true;
            // 
            // btnExportExcel
            // 
            this.btnExportExcel.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.btnExportExcel.Location = new System.Drawing.Point(382, 283);
            this.btnExportExcel.Name = "btnExportExcel";
            this.btnExportExcel.Size = new System.Drawing.Size(90, 23);
            this.btnExportExcel.TabIndex = 18;
            this.btnExportExcel.UseVisualStyleBackColor = true;
            this.btnExportExcel.Click += new System.EventHandler(this.exportExcel_Click);
            // 
            // m_subtitleLabel
            // 
            this.m_subtitleLabel.AutoSize = true;
            this.m_subtitleLabel.Location = new System.Drawing.Point(44, 36);
            this.m_subtitleLabel.Name = "m_subtitleLabel";
            this.m_subtitleLabel.Size = new System.Drawing.Size(0, 13);
            this.m_subtitleLabel.TabIndex = 1;
            // 
            // titleLabel
            // 
            this.titleLabel.AutoSize = true;
            this.titleLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.titleLabel.Location = new System.Drawing.Point(34, 9);
            this.titleLabel.Name = "titleLabel";
            this.titleLabel.Size = new System.Drawing.Size(0, 13);
            this.titleLabel.TabIndex = 0;
            // 
            // FrmDataSummary
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.tbCtrlDataSummary);
            this.Controls.Add(this.btnExportExcel);
            this.Name = "FrmDataSummary";
            this.Size = new System.Drawing.Size(518, 350);
            this.Load += new System.EventHandler(this.frmDataSummary_Load);
            this.Controls.SetChildIndex(this.btnExportExcel, 0);
            this.Controls.SetChildIndex(this.tbCtrlDataSummary, 0);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBoxIcon)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.rackGridView)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.coloGridView)).EndInit();
            this.tbPageServer.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.serverGridView)).EndInit();
            this.tbCtrlDataSummary.ResumeLayout(false);
            this.tbPageColo.ResumeLayout(false);
            this.tbPageRack.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        #region Methods
        /// <summary>
        /// This function returns the ADO classic XML recordset representation
        /// of sourceDataTable. To acheive this, the function calls the
        /// ConvertToAdoRecordset function to convert the ADO.NET DataTable 
        /// into an ADO recordset. It then uses the ADO recordset's save
        /// method for XML persistance.
        /// </summary>
        /// <param name="sourceDataTable">Reference to the ADO.NET 
        /// data table to convert</param>
        /// <returns>ADO classic XML representation of the data contained 
        /// in sourceDataTable</returns>
        public static string ConvertToAdoRecordsetXml(System.Data.DataTable sourceDataTable)      
        {
            // String to store AdoXML
            string targetAdoXmlString = string.Empty;
            
            // Ado recordset
            ADODB.Recordset targetRecordset;

            // Ado stream
            ADODB.StreamClass targetAdoXmlStream;

            try
            {
                // Object creation
                targetRecordset = new ADODB.RecordsetClass();
                targetAdoXmlStream = new ADODB.StreamClass();

                if(sourceDataTable == null)
                {                   
                  return null;
                }

                // Convert the ADO.NET DataTable into an ADO recordset
                targetRecordset = ConvertToAdoRecordset(sourceDataTable);

               // For Xml persistance
                targetRecordset.Save(targetAdoXmlStream,
                    ADODB.PersistFormatEnum.adPersistXML);

                // AdoXml string required by Visio to create datarecordset
                targetAdoXmlString = targetAdoXmlStream
                    .ReadText(targetAdoXmlStream.Size);
            }
            catch (Exception err)
            {
                System.Diagnostics.Debug.WriteLine(err.Message);
            }
            return targetAdoXmlString;
        }

        /// <summary>
        /// This function converts the incoming ADO.NET sourceDataTable 
        /// to an ADO targetRecordset by: 
        /// 1.  Iterating first through the DataTable's DataColumns, converting 
        ///	 them to Fields & mapping their types by using the mapping 
        ///	 in the translateType function.
        /// 2.  Then populating the targetDataRecordset's rows by iterating through 
        ///	 and copying the value of the sourceDataTable's rows. 
        /// </summary>
        /// <param name="sourceDataTable">Reference to the ADO.NET data 
        /// table to convert</param>
        /// <returns>An ADO classic DataRecordset representation of the data 
        /// contained in sourceDataTable</returns>
        public static ADODB.Recordset ConvertToAdoRecordset(System.Data.DataTable sourceDataTable)
        {
            // ADO Recordset to store data contained in sourceDataTable
            ADODB.Recordset targetRecordset = new ADODB.RecordsetClass();
            try
            {
                targetRecordset.CursorLocation =
                    ADODB.CursorLocationEnum.adUseClient;

                // Iterate through each DataColumn from the source data table
                // and create a new ADO field for each. 
                foreach (System.Data.DataColumn sourceColumn
                             in sourceDataTable.Columns)              
                {
                    // The targetRecordset.Fields.Append method requires
                    // a values for this attribute. 
                    ADODB.FieldAttributeEnum targetFieldAttribute;
                    if (sourceColumn.AllowDBNull)
                    {
                        targetFieldAttribute =
                            ADODB.FieldAttributeEnum.adFldIsNullable;
                    }
                    else
                    {
                        targetFieldAttribute =
                            ADODB.FieldAttributeEnum.adFldUnspecified;
                    }

                    targetRecordset.Fields.Append(
                        sourceColumn.ColumnName,
                        translateType(sourceColumn.DataType),
                        sourceColumn.MaxLength,
                        targetFieldAttribute,
                        null);
                }

                // Open the recordset for input.
                targetRecordset.Open(
                    System.Reflection.Missing.Value,
                    System.Reflection.Missing.Value,
                    ADODB.CursorTypeEnum.adOpenStatic,
                    ADODB.LockTypeEnum.adLockOptimistic,
                    0);

                // For each row in the source DataTable, create a new recordset
                // row and copy entries to the newly created row.
                foreach (System.Data.DataRow sourceRow in sourceDataTable.Rows)
                {                
                    targetRecordset.AddNew(
                        System.Reflection.Missing.Value,
                        System.Reflection.Missing.Value);

                    for (int i = 0; i < sourceDataTable.Columns.Count; i++)
                    {
                        if (!String.IsNullOrEmpty(sourceRow[i].ToString()))
                        {
                            targetRecordset.Fields[i].Value = sourceRow[i];
                        }
                        else
                            targetRecordset.Fields[i].Value = string.Empty;
                    }
                }

            }
            catch (Exception err)
            {
                System.Diagnostics.Debug.WriteLine(err.Message);
            }
            return targetRecordset;
        }
        
        /// <summary>
        /// This function converts ADO.NET data types to the nearest ADO 
        /// equivalents by using the UnderlyingSystemType method as
        /// a bridge.
        /// </summary>
        /// <param name="sourceColumnType">Type of the incoming ADO.NET 
        /// DataColumn</param>
        /// <returns>Equivalent ADO data type</returns>
        private static ADODB.DataTypeEnum translateType(System.Type sourceColumnType)
        {
            ADODB.DataTypeEnum onErrorReturn = ADODB.DataTypeEnum.adIUnknown;
            try
            {
                switch (sourceColumnType.UnderlyingSystemType.ToString())
                {
                    case "System.Boolean":
                        return ADODB.DataTypeEnum.adBoolean;

                    case "System.Byte":
                        return ADODB.DataTypeEnum.adUnsignedTinyInt;

                    case "System.Char":
                        return ADODB.DataTypeEnum.adChar;

                    case "System.DateTime":
                        return ADODB.DataTypeEnum.adDate;

                    case "System.Decimal":
                        return ADODB.DataTypeEnum.adCurrency;

                    case "System.Double":
                        return ADODB.DataTypeEnum.adDouble;

                    case "System.Int16":
                        return ADODB.DataTypeEnum.adSmallInt;

                    case "System.Int32":
                        return ADODB.DataTypeEnum.adInteger;

                    case "System.Int64":
                        return ADODB.DataTypeEnum.adBigInt;

                    case "System.SByte":
                        return ADODB.DataTypeEnum.adTinyInt;

                    case "System.Single":
                        return ADODB.DataTypeEnum.adSingle;

                    case "System.UInt16":
                        return ADODB.DataTypeEnum.adUnsignedSmallInt;

                    case "System.UInt32":
                        return ADODB.DataTypeEnum.adUnsignedInt;

                    case "System.UInt64":
                        return ADODB.DataTypeEnum.adUnsignedBigInt;

                    case "System.String":
                        return ADODB.DataTypeEnum.adVarChar;

                    default:
                        return ADODB.DataTypeEnum.adVarChar;
                }
            }
            catch (Exception err)
            {
                System.Diagnostics.Debug.WriteLine(err.Message);
            }
            return onErrorReturn;
        }

        /// <summary>
        /// Create datatable from Colo grid
        /// </summary>
        /// <returns>Colo data table</returns>
        private DataTable CreateDataTableFromColoGrid()
        {
            // temp datatable to store colo data
            DataTable tempTable = new DataTable();
            tempTable.Locale = CultureInfo.InvariantCulture;

            // String array to store grid row data
            string[] tableRow = new string[coloGridView.Columns.Count];

            // Add column headers to temp datatable
            for (int gridIndex = 0; gridIndex < coloGridView.Columns.Count; gridIndex++)
            {
                tempTable.Columns.Add(coloGridView.Columns[gridIndex].HeaderText);
            }

            // Get cologridview rows
            DataGridViewRowCollection dgvrcColo =  coloGridView.Rows;           

            // Add grid data cell by cell to string array
            for (int rowIndex = 0; rowIndex < coloGridView.Rows.Count - 1; rowIndex++)
            {
                for (int colIndex = 0; colIndex < coloGridView.Columns.Count; colIndex++)
                {                   
                    tableRow[colIndex] = (string)dgvrcColo[rowIndex].Cells[colIndex].Value;
                }

                // Add string array as row to temp table
                tempTable.Rows.Add(tableRow);

            }
            // temp datatable consists of colo data
            return tempTable;
        }

        /// <summary>
        /// Create datatable from Rack grid
        /// </summary>
        /// <returns>Rack data table</returns>
        private DataTable CreateDataTableFromRackGrid()
        {
            // temp datatable to store rack data
            DataTable tempTable = new DataTable();
            tempTable.Locale = CultureInfo.InvariantCulture;

            // String array to store grid row data
            string[] tableRow = new string[rackGridView.Columns.Count];

            // Add column headers to temp datatable
            for (int gridIndex = 0; gridIndex < rackGridView.Columns.Count; gridIndex++)
            {
                tempTable.Columns.Add(rackGridView.Columns[gridIndex].HeaderText);
            }

            // Get rackgridview rows
            DataGridViewRowCollection dgvrcRack = rackGridView.Rows; 

            // Add grid data cell by cell to string array
            for (int rowIndex = 0; rowIndex < rackGridView.Rows.Count - 1; rowIndex++)
            {
                for (int colIndex = 0; colIndex < rackGridView.Columns.Count; colIndex++)
                {
                    tableRow[colIndex] = (string)dgvrcRack[rowIndex].Cells[colIndex].Value;
                }

                // Add string array as row to temp table
                tempTable.Rows.Add(tableRow);

            }
            // temp datatable consists of rack data
            return tempTable;
        }

        /// <summary>
        /// Create datatable from Server grid
        /// </summary>
        /// <returns>Server data table</returns>
        private DataTable CreateDataTableFromServerGrid()
        {
            // temp datatable to store server data
            DataTable tempTable = new DataTable();
            tempTable.Locale = CultureInfo.InvariantCulture;

            // String array to store grid row data
            string[] tableRow = new string[serverGridView.Columns.Count];

            // Add column headers to temp datatable
            for (int gridIndex = 0; gridIndex < serverGridView.Columns.Count; gridIndex++)
            {
                tempTable.Columns.Add(serverGridView.Columns[gridIndex].HeaderText);
            }

            // Get servergridview rows
            DataGridViewRowCollection dgvrcServer = serverGridView.Rows; 

            // Add grid data cell by cell to string array
            for (int rowIndex = 0; rowIndex < serverGridView.Rows.Count - 1; rowIndex++)
            {
                for (int colIndex = 0; colIndex < serverGridView.Columns.Count; colIndex++)
                {
                    tableRow[colIndex] = (string)dgvrcServer[rowIndex].Cells[colIndex].Value;
                }

                // Add string array as row to temp table
                tempTable.Rows.Add(tableRow);
          
            }

            // Add blank columns to server datatable
            tempTable.Columns.Add(Global.ServerFieldIPAdress);
            tempTable.Columns.Add(Global.ServerFieldOperatingSystem);
            tempTable.Columns.Add(Global.ServerFieldProcessor);
            tempTable.Columns.Add(Global.ServerFieldCpuUsage);
            tempTable.Columns.Add(Global.ServerFieldTotalMem);
            tempTable.Columns.Add(Global.ServerFieldAvailMem);
            tempTable.Columns.Add(Global.ServerFieldIsVirtual);

            // temp datatable consists of server data
            return tempTable;
        }
        #endregion

        #region Events
        /// <summary>
        /// On Form load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmDataSummary_Load(object sender, EventArgs e)
        {
            try
            {
                // Header Panel Office Icon
                System.IO.Stream xsdResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("RackServerManager.Images.office.ico");
                base.pictureBoxIcon.Image = System.Drawing.Image.FromStream(xsdResourceStream);
            }
            catch (RackException ex)
            {

                // To catch and display exceptions from sub functions
                RackException.HandleExceptions(Global.GetResourceString("Exception_MsgBox_Caption"),
                            Global.GetResourceString("Exception_UsrMsg"), ex.InnerException);
            }
            catch (Exception ex)
            {
                // To catch exceptions from current functions
                RackException.HandleExceptions(Global.GetResourceString("Exception_MsgBox_Caption"),
                            Global.GetResourceString("Exception_UsrMsg"), ex);
            }
        }

        /// <summary>
        /// Handler for exporting excel 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void exportExcel_Click(object sender, EventArgs e)
        {
            try
            {
                this.FindForm().Cursor = Cursors.WaitCursor;

                // Export to excel consists of colo, rack, server grid data as parameters
                ExcelServiceManager.ExportToExcel(coloGridView, rackGridView, serverGridView);
            }
            catch (RackException ex)
            {
                // To catch and display exceptions from sub functions
                RackException.HandleExceptions(Global.GetResourceString("Exception_MsgBox_Caption"),
                            Global.GetResourceString("Exception_UsrMsg"), ex.InnerException);
            }
            catch (Exception ex)
            {
                // To catch exceptions from current functions
                RackException.HandleExceptions(Global.GetResourceString("Exception_MsgBox_Caption"),
                            Global.GetResourceString("Exception_UsrMsg"), ex);
            }
            finally
            {
                this.FindForm().Cursor = Cursors.Default;
            }

        }
        #endregion
    }
}